<?php
namespace  app\man\controller;

use think\Controller;
use think\Db;
use think\Paginator;
use think\Request;
use think\Cache;
class  Lead extends Common
{

	public function  txt()
	{
		$txt = Db::name('lead')->paginate();
        $config = Db::name('config')->where('display',1)->column('key,val');
        $this->assign('config',$config);
		$this->assign('txt',$txt);

		return $this->fetch('txt');
	}

	private function GetCache(){
        $cache = 'redis';
        if($_SERVER['HTTP_HOST']=='osman.cn'){
            $cache = 'file';
        }
        return $cache;
    }

	public function delete()
	{
		$id = input('aid');

		$res = Db::name('lead')->where('id',$id)->delete();

		if($res){
			return true;
		}else{
			return false;
		}
	}

	public function changeStatus(){
		$status = input('status');
		$res = Db::name('config')->where('key','dump_txt')->update(['val'=>$status]);
		$config = Db::name('config')->where('display',1)->column('key,val');
        Cache::store($this->GetCache())->set('osman_config',$config,600);
		if($res === false){
			return '操作失败';
		}else{
			if($status==1){
				$data = Db::name('gotos')->where('status',2)->column('domain');
		        $lead = Db::name('lead')->where('display',1)->column('name','id');
		        if(empty($data)||empty($lead)){
		            $string = '';
		        }else{
		        $str = '';
		        foreach($lead as $k=>$val){
		            shuffle($data);
		            $str .='<a  href="http://'.$data[0].'/leadlab/'.createrand(8).'/'.$k.'"><li>'.$val.'</li></a>';
		        }
    $string =<<<EOT
    var  sg ='<p style="color: red;font-weight: 900;margin-top: 15px;">更多精彩↓↓↓&gt;&gt;&gt;</p>';
        sg +='$str';
        document.writeln(sg);
EOT;
}
file_put_contents(ROOT_PATH.'/public/lead.js', $string);
			}else{
file_put_contents(ROOT_PATH.'/public/lead.js', '');
			}
			return '操作成功';
		}
	}


	public function  add_txt(){

		 if(Request::instance()->isPost()){
			$data=Request::instance()->post();
			$data['ptime']=date('Y-m-d H:i:s');
			$res=Db::name('lead')->insert($data);
			if($res){
				$options = [
		            'type'  =>  'Redis', 
		            'expire'=>  180, 
		            'prefix'=>  'think',
		            'host'  =>  '127.0.0.1',
		            'port'  => '6379'
		        ];
        		Cache::connect($options);
				cache::rm('osman_qywx_leads');
				$this->success('添加成功',url('man/lead/txt'));
			}else{
				$this->success('添加失败',url('man/lead/txt'));
			}
		}

		return $this->fetch('add_txt');
	}


	public function edit_txt(){
		$id=input('id');
		 if(Request::instance()->isPost()){
			$data=Request::instance()->post();
			$data['ptime']=date('Y-m-d H:i:s');
			unset($data['id']);
			$res=Db::name('lead')->where('id',$id)->update($data);
			if($res){
				$options = [
		            'type'  =>  'Redis', 
		            'expire'=>  180, 
		            'prefix'=>  'think',
		            'host'  =>  '127.0.0.1',
		            'port'  => '6379'
		        ];
        		Cache::connect($options);
				cache::rm('osman_qywx_leads');

				$this->success('修改成功',url('man/lead/txt'));
			}else{
				$this->success('修改失败',url('man/lead/txt'));
			}
		}

		$con = Db::name('lead')->where('id',$id)->find();
		$this->assign('con',$con);
		return $this->fetch('edit_txt');
	}

	

}


?>
